home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / ulaw.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.9 KB  |  123 lines

  1. /*
  2.     File:        ULAW.c
  3.  
  4.     Contains:    Routine demonstrating how to parse Sun's .au sound files.
  5.  
  6.     Written by: Mark Cookson    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "ULAW.h"
  25.  
  26. /* I threw this together in about an hour after just looking at the structure
  27.    of some .au files that I made from some known AIFF files.  This could use
  28.    some work to get it to deal with formats that I have no idea about, but
  29.    since I have no idea about them, I can't do that...
  30. */
  31.  
  32. /*-----------------------------------------------------------------------*/
  33.         OSErr    ASoundGetULAWHeader        (SoundInfoPtr theSoundInfo,
  34.                                         long *dataStart,
  35.                                         long *length)
  36. /*-----------------------------------------------------------------------*/
  37. {
  38.     ParamBlockRec            pb;
  39.     auHeader                ULAWHeader;
  40.     long                    filePosition        = kInit,
  41.                             byteCount            = kInit,
  42.                             sampleSize            = kInit,
  43.                             tempLength            = kInit;
  44.     Fixed                    sampleRate            = kInit;
  45.     OSErr                    err                    = noErr;
  46.  
  47.     *dataStart = kInit;
  48.  
  49.     err = SetFPos (theSoundInfo->refNum, fsFromStart, filePosition);
  50.     if (err != noErr) {
  51.         DebugPrint ("\pSetFPos failed!");
  52.     }
  53.     else {
  54.         byteCount = sizeof (auHeader);
  55.         err = FSRead (theSoundInfo->refNum, &byteCount, &ULAWHeader);
  56.         if ((err != noErr) && (err != eofErr)) {
  57.             DebugPrint ("\pFSRead failed!");
  58.         }
  59.         else {
  60.             sampleRate = ASoundLongDoubleToFix (ULAWHeader.sampleRate);
  61.             *dataStart = ULAWHeader.offsetToData;
  62.             *length = ULAWHeader.dataSize;
  63.  
  64.             /* Get the length of the file because SoundEdit stores the uncompressed length of a file
  65.                and not all Sun/NeXT files have the compressed length of the file in the dataSize
  66.                field.  We depend on having the right length (the compressed length). */
  67.  
  68.             pb.ioParam.ioCompletion    = nil;
  69.             pb.ioParam.ioRefNum        = theSoundInfo->refNum;
  70.             err = PBGetEOF (&pb, false);
  71.             err = pb.ioParam.ioResult;
  72.             if (err == noErr) {
  73.                 tempLength = (long)pb.ioParam.ioMisc;
  74.             }
  75.             if (*length > tempLength || *length == -1) {    /* This would mean that the uncompressed length was stored || no length was stored */
  76.                 *length = tempLength - ULAWHeader.offsetToData;    /* We want the compressed length */
  77.             }
  78.  
  79.             switch (ULAWHeader.dataFormat) {
  80.                 case SND_FORMAT_MULAW_8:
  81.                     err = SetupDBHeader (theSoundInfo,
  82.                                         sampleRate,
  83.                                         k16BitSample,
  84.                                         ULAWHeader.numChannels,
  85.                                         fixedCompression,
  86.                                         kULawCompression);
  87.                     theSoundInfo->needsMasking = false;
  88.                     break;
  89.                 case SND_FORMAT_LINEAR_8:
  90.                     err = SetupDBHeader (theSoundInfo,
  91.                                         sampleRate,
  92.                                         k8BitSample,
  93.                                         ULAWHeader.numChannels,
  94.                                         notCompressed,
  95.                                         NoneType);
  96.                     theSoundInfo->needsMasking = true;
  97.                     break;
  98.                 case SND_FORMAT_LINEAR_16:
  99.                     err = SetupDBHeader (theSoundInfo,
  100.                                         sampleRate,
  101.                                         k16BitSample,
  102.                                         ULAWHeader.numChannels,
  103.                                         notCompressed,
  104.                                         NoneType);
  105.                     theSoundInfo->needsMasking = false;
  106.                     break;
  107.                 default:
  108.                     DebugPrint ("\pUnknown or unplayable encoding format");
  109.                     err = kUnknownFormat;
  110.                     break;
  111.             }
  112.         }
  113.     }
  114.  
  115.     if (err != noErr) {
  116.         DebugPrint ("\pError in ASoundGetULAWHeader");
  117.     }
  118.  
  119.     *length += *dataStart;    /* Otherwise we wouldn't read the last few bytes from the end of the sound. */
  120.  
  121.     return err;
  122. }
  123.